// pages/collect/collect.js
const app = getApp()
const util = require('../../utils/util.js')
Page({
     data: {
             list: [],    //收藏渲染reverse()
             pages: 1,//页数            
             type:''  ,   // type：1--最近视频；2--我的收藏；3--我的视频
            
     },
        onLoad: function (options){
                if (options.type == 1) {
                        this.setData({
                                infotip:" 您还没有观看任意视频",
                                title: '最近播放'
                        })
                } else if (options.type == 2) {
                        this.setData({
                                infotip: "您还没有收藏任意视频",
                                title: '我的收藏',

                        })
                } else if (options.type == 3) {
                        this.setData({
                                title: '我的视频',
                                infotip: "您还没上传视频",
                                userName: wx.getStorageSync('userInfo').userName,
                                userUrl: wx.getStorageSync('userInfo').userImgUrl
                        })
                }
                this.setData({
                        type: options.type,
                        headerheight: app.globalData.navHeight
                })
     },
     onShow:function(){
             var that = this
             that.request()
     },
        //      更多视频
        backindex: function () {
                wx.reLaunch({
                        url: '/pages/index/index',
                })
        },
        login: function () {
                var that = this
                wx.login({
                        success: function (res) {
                                var code = res.code
                                var data = {
                                        userCode: code,
                                        userImageUrl: "",
                                        userName: ""
                                }
                                util.promotion(app.globalData.appurl + "/qinqiang/user", "POST", data, that.succeed)
                        }
                })
        },
        //点击客服
        kefu:function(){
                this.setData({
                        kefushow:true
                })
        },
        succeed: function (res) {
                wx.setStorageSync('tekon', res.data.token)
                wx.setStorageSync('userInfo', res.data.user)
                this.request()
        },
        request: function () {
                var that = this
                var pages = that.data.pages
                wx.showLoading({
                        title: '加载中...',
                })
                if(pages==1){
                        that.setData({
                                list: []
                        })
                }
                var data = {
                        userId: wx.getStorageSync('userInfo').id,
                        pageIndex: pages,
                        pageSize: 10,
                };
                var header = {
                        'authorization': wx.getStorageSync('tekon'),
                        'content-type': "application/x-www-form-urlencoded"
                }
                if(that.data.type==1){
                        //最近播放
                        util.formhttp(app.globalData.appurl +'/qinqiang/history', 'get', data, header, that.favorite);
                } else if (that.data.type == 2){
                        //我的收藏
                        util.formhttp(app.globalData.appurl + '/qinqiang/favorite', 'get', data, header, that.favorite);
                } else if (that.data.type == 3){
                        //我的视频
                        util.formhttp(app.globalData.appurl + '/qinqiang/myVideo', 'get', data, header, that.favorite);
                }
               
        },
        favorite:function(res){
                console.log(res)
                if (res.status == 401) {
                        this.login()
                }
                var that=this
                var pages = that.data.pages
                var reslist = res.data
                var box = that.data.list;
                wx.hideLoading()
                if (pages > 1) {
                        if (reslist.length == 0) {
                                that.setData({
                                        info: "无更多内容"
                                })
                        }
                }
                for (var i = 0; i < reslist.length; i++) {
                        box.push(reslist[i]);
                }
                var b = {};
                var New = new Array();
                if (that.data.type == 1) {
                        box.forEach(v => {
                                !b[v.watchTime] ? (b[v.watchTime] = [v]) : b[v.watchTime].push(v);
                        })
                } else if (that.data.type == 2) {
                        box.forEach(v => {
                                !b[v.favorTime] ? (b[v.favorTime] = [v]) : b[v.favorTime].push(v);
                        })
                } else if (that.data.type == 3) {
                        box.forEach(v => {
                                !b[v.createTime.substring(0, 10)] ? (b[v.createTime.substring(0, 10)] = [v]) : b[v.createTime.substring(0, 10)].push(v);
                        })
                }
                var i = 0;
                for (var o in b) {
                        New[i] = {
                                "time": o,
                                "oarr": b[o]
                        }
                        i++;
                }
                that.setData({
                        box: New
                })
        },
        onReachBottom: function () {
                var that = this
                var pages = that.data.pages
                pages++
                that.setData({
                        pages: pages,
                })
                that.request()
        }
})